package cmj.cloudmusic.activity;

import android.graphics.PixelFormat;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import java.io.File;
import java.io.IOException;

import cmj.cloudmusic.R;

public class MMMMActivity extends AppCompatActivity implements View.OnClickListener {


    MediaRecorder mediaRecorder;

    SurfaceView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mmmm);
        Button button= (Button) findViewById(R.id.button);
        Button button2= (Button) findViewById(R.id.button2);
        Button button3= (Button) findViewById(R.id.button3);
        Button button4= (Button) findViewById(R.id.button4);
        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);

        view= (SurfaceView) findViewById(R.id.surfaceView);

        mediaRecorder=new MediaRecorder();
        //横屏显示
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        //设置全屏显示
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //设置半透明
        getWindow().setFormat(PixelFormat.TRANSLUCENT);

        //设置surfaceview
        //设置分辨率
        view.getHolder().setFixedSize(1280,720);
        //禁止屏幕自动关闭
        view.getHolder().setKeepScreenOn(true);



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:

                //录入源
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//输出格式
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//编码格式
                //存储位置
                try {
                    File file= new File(Environment.getExternalStorageDirectory().getCanonicalPath(),"my.3gp");
                    mediaRecorder.setOutputFile(file.getAbsolutePath());
                    mediaRecorder.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaRecorder.start();
                break;
            case R.id.button2:
                mediaRecorder.stop();
                mediaRecorder.reset();
                mediaRecorder.release();
                break;
            case R.id.button3:
                startRecord();
                break;
            case R.id.button4:
                stop();
                break;

        }
    }


    public  void  startRecord(){
        //麦克风录制音频
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        //摄像头录制视频
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        //啊设置录制参数
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        //设置编码
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
        //设置录入的视频大小
        mediaRecorder.setVideoSize(176,144);
        //设置视频帧大小
        mediaRecorder.setVideoFrameRate(20);
//        //设置宽高
//        mediaRecorder.setVideoSize(177,152);
        //设置帧数
//        mediaRecorder.setVideoFrameRate(20);
        try {

            File file=new File(Environment.getExternalStorageDirectory().getCanonicalPath(),"wodeshipin.mp4");
            mediaRecorder.setOutputFile(file.getAbsolutePath());

            mediaRecorder.setPreviewDisplay(view.getHolder().getSurface());

            mediaRecorder.prepare();

        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaRecorder.start();

    }
    public void stop(){
        mediaRecorder.stop();
        mediaRecorder.reset();
        mediaRecorder.release();
    }
}
